var curr_page = 1;
var per_page = 16;
var num_pages = Math.ceil(searchresults.length / per_page);
if(num_pages == 0){
	num_pages = 1;
}



function makePaginator(){
	$("#paginator").paginate({
		count: num_pages,
		start: curr_page,
		display: 5,
		border: true,
		border_color: '#CD9F33',
		text_color: '#6F4632',
		background_color: '#FAF2E1',	
		border_hover_color: '#B18110',
		text_hover_color: '#6F4632',
		background_hover_color: '#DCC388', 
		images: false,
		mouse: 'press',
		onChange: function(page){
			DisplayPage(page);
			positionPaginator();
			$(".resize-150").load(function(){
				resize($(this), 150);
			});
			replaceIdByName();
			replaceIdByPic();
		}
	});
}

function DisplayPage(page){			
	var html = '';
	num = 0;
	curr_page = page;
	for (var i = 0; i < per_page; i++){
		var counter = ((page - 1) * per_page) + i;
		
		if (typeof(searchresults[counter]) == "undefined"){
			if(html == ''){
				html += '<br /><span class="medium-text">No items found. <br /><br />Try another Search!</span>';
			}
			break;
		}
		
		if(num % 4 == 0){
			html += '<div class="row-container">';
		}
		
		html += '<div class="item-container">';
		html += '<div class="img-container"><a href="'+base_url+'itemdescription/' + searchresults[counter].offer_id + '"><img class="resize-150" src="'+base_url+'resources/offer_images_thumbnail/' + searchresults[counter].item_pic + '" title="'+searchresults[counter].item_title+'"/></a></div>';
		html += '<div class="cnt-container normal-text" style="font-size:11pt">';
		html += searchresults[counter].item_title.substr(0, 20);
		if (searchresults[counter].item_title.length > 20){
			html += '...';
		}
		html += '<br />';
		html += 'Price: ';
		if(searchresults[counter].item_price == '0.00'){
			html += 'Free<br />';
		}
		else{
			html += '$' + searchresults[counter].item_price + '<br />';
		}
		html += 'Status: ' + searchresults[counter].status + '<br />';
		html += '</div></div>';
		
		if(num % 4 == 3){
			html += '<div style="clear:both"></div></div>';
		}		
		num++;
	}
	if(num % 4 != 0){
		html += '<div style="clear:both"></div></div>';
	}
	
	$("#items-grid").html(html);
}


function positionPaginator(){
	var width = $('#items-grid').width();
	var length = num_pages > 5 ? 5 : num_pages;
	var actual = 60 + 60 + 25 * num_pages;
	$('#paginator').css('margin-left', (width - actual) / 2);
}


$(function(){
	makePaginator();
	DisplayPage(curr_page);
	positionPaginator();
});